package com.kuangchi.weixin.sginin.controller.Impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.fastjson.JSONObject;
import com.kuangchi.weixin.sginin.controller.IRoutingController;
import com.kuangchi.weixin.sginin.utils.HttpUtils;
import com.kuangchi.weixin.sginin.vo.KcSgininExcelVo;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URLEncoder;

/**
 * @version v1.0
 * @类描述：
 * @项目名称：
 * @包名： com.kuangchi.weixin.sginin.controller.Impl
 * @类名称：SgininController
 * @创建人：YIHANG.XU
 * @创建时间：2020/10/1310:55 下午
 * @修改人：YIHANG.XU
 * @修改时间：2020/10/1310:55 下午
 * @修改备注：
 * @Copyright
 */
@RestController
@RequestMapping("/oauth2")
public class RoutingController implements IRoutingController {

    String APPID = "wx2af00191fa5db50e";

    String SECRET = "4f75272841cae87325e97a4946ccbd4d";

    String clientHostUrl = "http://shkc.kuang-chi.com";
    

    @Override
    @GetMapping("/getRedirectUrl/{path}")
    public String weixinRediect(@PathVariable("path") String path) {
        if (path.equals("signUp")) {
            return "redirect:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx2bd862360ff20cb6&redirect_uri=http://shkc.kuang-chi.com/oauth2/signUpPathOpenId&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
        } else {
            return "redirect:https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx2bd862360ff20cb6&redirect_uri=http://shkc.kuang-chi.com/oauth2/signInPathOpenId&response_type=code&scope=snsapi_base&state=123#wechat_redirect";
        }
    }

    @Override
    @RequestMapping(value = "/signUpPathOpenId", method = RequestMethod.GET)
    public void signUpPathOpenId(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        getOpenId(httpServletRequest, httpServletResponse);
        try {

            httpServletResponse.sendRedirect(getIP(clientHostUrl) + "/ssr/signup.html");

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private String getIP(String url) {
        URI uri = URI.create(url);
        URI effectiveURI;
        try {
            effectiveURI = new URI(uri.getScheme(), uri.getUserInfo(), uri.getHost(), uri.getPort(), null, null, null);
            return effectiveURI.toString();
        } catch (URISyntaxException e) {
            return url;
        }
    }

    @Override
    @RequestMapping(value = "/signInPathOpenId", method = RequestMethod.GET)
    public void signInPathOpenId(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        getOpenId(httpServletRequest, httpServletResponse);
        try {

            httpServletResponse.sendRedirect(getIP(clientHostUrl) + "/ssr/signIn.html");

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    private String getOpenId(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse) {
        String CODE = httpServletRequest.getParameter("code");
        //换取access_token 其中包含了openid
        String URL = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code".
                replace("APPID", APPID).replace("SECRET", SECRET).replace("CODE", CODE);
        //URLConnectionHelper是一个模拟发送http请求的类
        String jsonStr = null;
        try {
            jsonStr = HttpUtils.doGet(URL);
        } catch (Exception e) {
            e.printStackTrace();
        }
        JSONObject jsonObj = JSONObject.parseObject(jsonStr);
        String openid = jsonObj.get("openid").toString();
        Cookie cookie = new Cookie("openId", openid);
        cookie.setPath("/");
        cookie.setDomain("shkc.kuang-chi.com");
        cookie.setMaxAge(Integer.MAX_VALUE);
        httpServletResponse.addCookie(cookie);
        return openid;

    }


}
